/*

Copyright (c) by Emil Valkov,
All rights reserved.

License: http://www.opensource.org/licenses/bsd-license.php

*/

#include <cv.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>

#include "RaspiCamCV.h"

#include <stdio.h>
#include <ctime>
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <vector> 
#include <string>

using namespace std;
using namespace cv;


// Important Consts
const int RED 	= 2;
const int GREEN = 1;
const int BLUE  = 0;

/*vector<Mat> getRedBinaryImage(Mat &image, vector<int> colors){
	vector<Mat> chans;
	// Create 
	for (int i = 0; i < colors.size(); i++){
		chan.push_back(Mat(image.rows,image.cols,CV_8UC1));
	}
	chans.push_back(Mat(image.rows,image.cols,CV_8UC1));	
	chans.push_back(Mat());
	chans.push_back(Mat());
	//Mat * hue = new Mat();
	//Mat * sat = new Mat();
	//Mat * itn = new Mat();
	
	split(image,chans);
	
	return *chans[0];

}*/



int main(int argc, const char** argv){
	RaspiCamCvCapture * capture = raspiCamCvCreateCameraCapture(0); // Index doesn't really matter
	//cvNamedWindow("RaspiCam HSV", 1);
	Mat image; 
	//IplImage * temp;
	timeval a;
	timeval b;

	uchar * ptr = NULL;
	uchar * out = new uchar[8];

	gettimeofday(&a,0);
	bool condition = true;
	int ** average;
	image = Mat(raspiCamCvQueryFrame(capture));
	int count = 0;
	vector<int> iwParams;
	iwParams.push_back(CV_IMWRITE_JPEG_QUALITY);
	iwParams.push_back(100);
	while(condition){
		Mat image = Mat(raspiCamCvQueryFrame(capture));
		//printf("Width %d, height %d\n",image->width,image->height);
		imshow("RaspiCam RGB",image);
		//cvtColor(temp,image,CV_RGB2BGR);
		
		
		//Mat im = getRedBinaryImage(image,0);
		//imshow("RaspiCam HSV",im);
		imwrite(string("im")+(char)('0'+count)+".jpg",image,iwParams);	
		cvWaitKey(0);
		
		
		//condition = false;
		count++;
		  
		  
	}
	gettimeofday(&b,0);

	cout << "diff: " << (b.tv_sec - a.tv_sec) << endl;



	destroyWindow("RaspiCam HSV");
	//cvReleaseImage(&image);
	raspiCamCvReleaseCapture(&capture);
	return 0;
}
